home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1404 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.9 KB

  1. Path: news.rain.org!usenet
  2. From: "Guus Leeuw jr." <guusl@eiffel.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Deleting arrays of garbage collectables
  5. Date: Wed, 10 Jan 1996 13:19:15 -0800
  6. Organization: ISE Inc. http://www.eiffel.com
  7. Message-ID: <30F42D53.3B4D1687@eiffel.com>
  8. References: <4d0mbf$6on@news.ox.ac.uk>
  9. NNTP-Posting-Host: @outback.eiffel.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0b3 (X11; I; Linux 1.2.8 i586)
  14.  
  15. Michael Brewer wrote:
  16. > Probably a bad subject line, but here is what I mean:
  17. > I am using a bunch of classes that can be shared by the mechanism:
  18. > class Allo; // defined elsewhere
  19. > Allo *hi = new Allo;
  20. > hi->ref(); // Using this object
  21. > //.....  code
  22. > hi->unref(); // finished with it. If it hasn't been ref()'ed
  23. >              // elsewhere, it will be deleted
  24. > Now what if I want an array of these? There is no satisfactory default
  25. > constructor, so I need an array of pointers:
  26. > Allo **arr = new Allo * [10];
  27. > for(int i = 0; i < 10; i++)
  28. > {
  29. >     arr[i] = new Allo(3.221);
  30. >     arr[i]->ref();
  31. > }
  32. > // ......... code
  33. > // ....
  34. > // finished, unref():
  35. > for(int i = 0; i < 10; i++)
  36. >     arr[i]->unref();
  37. > OK, so now the arr[i] memory has been dealt with, but what about the
  38. > array of pointers arr?? There is no way of telling whether they still
  39. > point to something, since some other object may still be sharing the
  40. > data.
  41. > Is there a way to free up that memory?
  42.  
  43. I think there is, it'll cost a bit of work though.
  44.  
  45. I think you should write your own Array class (template (!)) which
  46. provides exactly the same mechanism. Form time to time you call a member
  47. function that checks whether there are still pointers that are ref'ed in
  48. the array. If not the object's memory will be freed, etc. etc.
  49. > Thanks.
  50. > Mike
  51.  
  52. Hope this helps and regards,
  53.     Guus Leeuw jr.
  54.